/*
 * MCAL_UART.c
 *
 *  Created on: Sep 24, 2025
 *      Author: ADMIN
 */

#include "MCAL_UART.h"

UART_ID_Type UART_ID_0x01;
UART_ID_Type UART_ID_0x03;  

void uart_putchar(char c) {
	Printf_UART_Type.SendByteArr = (uint8) c;
	Printf_UART_Type.Send_ByteDate(&Printf_UART_Type);
}

/* This Function is used to initialize the usart */
Std_ReturnType MCAL_USART_Init(void)
{
	USART_Creat(&UART_ID_0x01, &huart1);
	set_putchar_function(uart_putchar);

	USART_Creat(&UART_ID_0x03, &huart3);
	return E_OK;
}

/* This Function is used to Reinitialize the usart */
Std_ReturnType MCAL_USART_DeInit(void)
{
    HAL_UART_MspDeInit(UART_ID_0x01.uartHandle);
    HAL_UART_MspDeInit(UART_ID_0x03.uartHandle);
    return E_OK;
}

/* This Function is used to Send one byte date by usart */
Std_ReturnType USART_SendByteDate(UART_ID_Type* uart_num)
{
    HAL_UART_Transmit(uart_num->uartHandle,(uint8*)&uart_num->SendByteArr,1,1000);
	return E_OK;
}

/* This Function is used to Send date by usart */
Std_ReturnType USART_SendDate(UART_ID_Type* uart_num)
{
    if (uart_num->SendDateIndex != NULL)
    {
        HAL_UART_Transmit(uart_num->uartHandle,(uint8*)uart_num->SendDateIndex,uart_num->SendByteLen,1000);
        return E_OK;
    }
    else
    {
        return E_NOT_OK;
    }
}

/* This Function is used to create a new usart */
void USART_Creat(UART_ID_Type* uart_num, UART_HandleTypeDef* uartHandle)
{
    /* SW information */
    uart_num->uartHandle = uartHandle;

    /* Send configure information */
    uart_num->SendByteArr = 0;
    uart_num->SendDateIndex = NULL;
    uart_num->SendByteLen =0;

    /* Receive configure information */
    uart_num->ReceiveByteArr =0;
    uart_num->ReceiveDateIndex = NULL;
    uart_num->ReceiveByteCnt =0;
    uart_num->ReceiveByteLen =0;

    /* Function Index */
    uart_num->Send_ByteDate = USART_SendByteDate;
    uart_num->Send_Date =  USART_SendDate;

    HAL_UART_Receive_IT(uart_num->uartHandle, (uint8_t *)&uart_num->ReceiveByteArr, 1);
}


